package com.omega_r.omegarecyclerview.pagination_example;


import com.omega_r.libs.omegarecyclerview.base.BaseProvider;
import com.omega_r.libs.omegarecyclerview.base.BaseViewHolder;
import com.omega_r.omegarecyclerview.ResourceTable;
import com.omega_r.omegarecyclerview.swipe_menu_example.Contacts;
import com.omega_r.omegarecyclerview.swipe_menu_example.ContactsViewHolder;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;
import java.util.List;

public class PaginationAbility extends Ability implements ImageProvider.Callback{

    private EventHandler mHandler = new EventHandler(EventRunner.getMainEventRunner());
    private ImageProvider adapter;
    private List<Image> imageList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_pagination);
        ListContainer omegaRecyclerView = (ListContainer) findComponentById(ResourceTable.Id_recyclerview);
        Component footer = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_item_pagination,null,false);
        imageList = Image.createImageList(0);
        adapter = new ImageProvider(getContext(), Image.createImageList(0)
                , ResourceTable.Layout_item_image);
        adapter.setCallback(this);
        adapter.setFooterComponent(footer);
        adapter.setComponentBindingListener(new BaseProvider.ComponentBindingListener() {
            @Override
            public BaseViewHolder creatViewHolder(Component item) {
                return new ImageViewHolder(item);
            }

            @Override
            public void initData(BaseViewHolder baseViewHolder, Object object, int position) {
                ((ImageViewHolder) baseViewHolder).updateView((Image) object,position);
            }
        });
        omegaRecyclerView.setItemProvider(adapter);
        omegaRecyclerView.setScrollListener(new ListContainer.ScrollListener() {
            @Override
            public void onScrollFinished() {
                if (omegaRecyclerView.getItemPosByVisibleIndex(omegaRecyclerView.getVisibleIndexCount()-1)==imageList.size()){
                    if (imageList.size()!=5) {
                        downloadItems();
                    }
                }
            }
        });
    }


    @Override
    public void onRetryClicked() {
        downloadItems();
    }

    private void downloadItems() {
        if (imageList.size()==5){
            adapter.setError();
        }else if (imageList.size()==20){
            adapter.removeFooterComponent();
        }else {
            adapter.setProgress();
        }
        mHandler.postTask(new Runnable() {
            @Override
            public void run() {
                imageList.addAll(Image.createImageList(5));
                adapter.addAllProviderList(Image.createImageList(5));

            }
        }, 3000);
    }

}
